package com.tea.zxql.component;

import cn.dev33.satoken.stp.StpInterface;
import com.tea.zxql.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassDescription:
 * @ClassName: StpInterfaceImpl
 * @JdkVersion: 17
 * @Author: HYC
 * @Created: 2025/7/18 15:17
 * @Version 1.0
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;


    /**
     * 获取用户权限列表
     * @param loginId 登录用户ID
     * @param loginType 登录类型
     * @return 用户权限列表
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String permissions = redisTemplate.opsForValue().get(loginId + ".permissions:");
        // 去除两边的方括号并分割字符串
        String trimmed = permissions.substring(1, permissions.length() - 1); // 去除 []
        String[] parts = trimmed.split(","); // 按逗号分割
        // 去除每个元素周围的空格并转换为List
        List<String> list = new java.util.ArrayList<>();
        for (String part : parts) {
            list.add(part.trim());
        }
        return list;
    }

    /**
     * 获取用户角色列表
     * @param o 登录用户ID对象
     * @param s 登录类型
     * @return 用户角色列表
     */
    @Override
    public List<String> getRoleList(Object o, String s) {
        Integer loginId = Integer.valueOf(o.toString());
        List<String> role = userMapper.getRole(loginId);
        return role;
    }
}

